package com.westa.smallshop.user;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.westa.smallshop.R;
import com.westa.smallshop.common.BaseActivity;
import com.westa.smallshop.common.BaseListAdapter;
import com.westa.smallshop.common.BaseListFragment;
import com.westa.smallshop.common.BasePresenter;
import com.westa.smallshop.common.GetPresenterInterface;
import com.westa.smallshop.common.RecyclerViewBaseAdapter;
import com.westa.smallshop.data.GoodsItem;
import com.westa.smallshop.goodsadd.GoodsAddActivity;
import com.westa.smallshop.goodsdetail.GoodsDetailActivity;
import com.westa.smallshop.setting.SettingActivity;
import com.westa.smallshop.util.DensityUtils;
import com.westa.smallshop.util.ListUtils;
import com.westa.smallshop.util.ScreenUtils;

import java.util.List;

import butterknife.BindString;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import jp.wasabeef.blurry.Blurry;

import static com.google.common.base.Preconditions.checkNotNull;


public class UserInfoFragment extends BaseListFragment<UserInfoContract.Presenter, GoodsItem>
        implements UserInfoContract.View, GetPresenterInterface, RecyclerViewBaseAdapter.OnItemClickListener {

    public static final int REQUEST_GOODS_ADD_FROM_USER_CODE = 1324;

    @BindView(R.id.iv_bg)
    ImageView mIvBg;
    @BindView(R.id.fl_iv_bg)
    FrameLayout mFlIvBg;
    @BindView(R.id.iv_user_portrait)
    CircleImageView mIvUserPortrait;
    @BindView(R.id.rl_user_header)
    RelativeLayout mRlUserHeader;
    @BindString(R.string.tab_user)
    String mTitle;
    @BindView(R.id.tv_no_content)
    TextView mTvNoContent;
    @NonNull
    private UserInfoContract.Presenter mPresenter;
    private UserInfoAdapter mAdapter;

    public static UserInfoFragment newInstance() {
        return new UserInfoFragment();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public int getCreateViewLayoutRes() {
        return R.layout.fragment_user;
    }

    @Override
    public void initView() {
        super.initView();
        getActivity().setTitle(mTitle);
        setRefreshLayoutEnabled(false);
        mTvNoContent.setTranslationY(100);
        mRlUserHeader.getLayoutParams().height =
                (int) (ScreenUtils.getScreenWidth(getContext()) * 0.6);
        Glide.with(this).load(R.drawable.ic_user_default_portrait)
                .asBitmap()
                .centerCrop()
                .error(R.drawable.ic_user_default_portrait)
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        mIvUserPortrait.setImageBitmap(resource);
                    }
                });
        Glide.with(this).load(R.drawable.iv_bg_user_header)
                .centerCrop()
                .into(new SimpleTarget<GlideDrawable>() {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        mIvBg.setImageDrawable(resource);
                        mFlIvBg.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                            @Override
                            public boolean onPreDraw() {
                                mFlIvBg.getViewTreeObserver().removeOnPreDrawListener(this);
                                Blurry.with(getContext()).radius(25).sampling(2).onto(mFlIvBg);
                                return true;
                            }
                        });
                    }
                });
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            getActivity().setTitle(mTitle);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public boolean isActive() {
        return isAdded();
    }

    @Override
    public void refreshData(List<GoodsItem> list) {
        if (ListUtils.isEmpty(list)) {
            mTvNoContent.setVisibility(View.VISIBLE);
        } else {
            mTvNoContent.setVisibility(View.GONE);
        }
        mAdapter.refreshData(list);
    }

    @Override
    public void loadMoreData(List<GoodsItem> list) {

    }

    @Override
    public void getDataFailed(String errorMsg) {

    }

    @NonNull
    @Override
    public BaseListAdapter getAdapter() {
        if (mAdapter == null) {
            mAdapter = new UserInfoAdapter(this);
            mAdapter.setOnItemClickListener(this);
            getRecyclerView().setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
            final int m = DensityUtils.dp2px(getContext(), 3.0f);
            getRecyclerView().addItemDecoration(new RecyclerView.ItemDecoration() {
                @Override
                public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                    outRect.set(m, m, m, m);
                }
            });
        }
        return mAdapter;
    }

    @Override
    public void refreshData() {
        mPresenter.refreshData();
    }

    @Override
    public void loadMoreData(int currentPage) {

    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.user_menu, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_settings:
                SettingActivity.startActivity(getActivity());
                break;
        }
        return true;
    }

    @Override
    public BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public void setPresenter(@NonNull UserInfoContract.Presenter presenter) {
        mPresenter = checkNotNull(presenter);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == BaseActivity.RESULT_OK_REFRESH) {
            refreshData();
        }
    }

    @OnClick({R.id.iv_bg, R.id.fl_iv_bg, R.id.iv_user_portrait, R.id.rl_user_header})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_bg:
                break;
            case R.id.fl_iv_bg:
                break;
            case R.id.iv_user_portrait:
                break;
            case R.id.rl_user_header:
                break;
        }
    }

    @Override
    public void onItemClick(View view, int position) {
        super.onItemClick(view, position);
        GoodsDetailActivity.startActivity(getActivity(), mAdapter.getItemObject(position).getGoods_id());
    }

    @OnClick(R.id.fab_add_goods)
    public void onClick() {
        GoodsAddActivity.startActivity(this, REQUEST_GOODS_ADD_FROM_USER_CODE);
    }
}
